<template>
	<view>
		<zmm-watermark :watermark="userStore.watermark" v-if="userStore.watermark"></zmm-watermark>
		<!--导航栏-->
		<pageLayout
			:barTitle="title"
			:isSearchNull="isSearchNull"
			:isOpen="isOpen"
			@rightClick="clickSearch">
		</pageLayout>
		<!--查询弹窗-->
		<up-popup :z-index="90" :show="isOpen" @close="closeSearch" mode="top">
			<view @touchmove.stop.prevent>
			<scroll-view class="search" scroll-y="true" @touchmove.stop>
				<view class="search-form">
					<uni-forms ref="searchRef" :modelValue="searchModel" label-width="160rpx">
						<uni-forms-item label="区域">
							<uni-data-select
								v-model="searchModel.regionCode"
								placeholder="请选择区域"
								:localdata="setStore.allProvinceList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="类型名称" v-if="checkPermi(['zhouyz:productType:list'])">
							<uni-data-select
								v-model="searchModel.typeId"
								placeholder="请选择商品类型名称"
								:localdata="productTypeList"></uni-data-select>
						</uni-forms-item>
						<uni-forms-item label="供应商名称" v-if="!checkOnlyRole(['supplier'])">
							<uni-easyinput v-model="searchModel.supplierName" placeholder="请输入供应商名称" />
						</uni-forms-item>
						<uni-forms-item label="商品名称">
							<uni-easyinput v-model="searchModel.productName" placeholder="请输入商品名称" />
						</uni-forms-item>
					</uni-forms>
					<view class="search-footer main-flex">
						<up-button
							class="search-footer-item"
							text="搜索"
							type="primary"
							@click="searchSubmit"
							icon="search"></up-button>
						<up-button
							class="search-footer-item"
							@click="resetSubmit"
							text="重置"></up-button>
					</view>
				</view>
			</scroll-view>
			</view>
		</up-popup>
		<view class="content">
			<!--操作-->
			<view class="operate main-flex">
				<view class="operate-item" v-if="checkPermi(['zhouyz:supplierProduct:add'])">
					<up-button
						class="operate-btn main-flex"
						size="mini"
						type="primary"
						@click="editFun()">
						<uni-icons class="operate-btn-icon" type="plusempty" size="28rpx" color="#ffffff"></uni-icons>
						<view class="operate-btn-text">新增</view>
					</up-button>
				</view>
			</view>
			<!--内容-->
			<view class="table">
				<ty-table
					:data="tableList"
					:columns="columns"
					:sortable="true"
					@row-click="rowClick"
					:cellClassName="() => 'table-cell'"
					:headerCellClassName="() => 'table-cell'"
					:rowClassName="() => 'table-row'"
					:isOperateBtn="(row) => {
						return checkPermi(['zhouyz:supplierProduct:edit', 'zhouyz:supplierProduct:remove'])
					}"
					stripe
					border>
					<template v-slot:supplier="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">供应商：<text class="table-td-text">{{row.supplierName}}</text></view>
							<view class="table-td-row main-flex">区域：<uni-tag size="small" :text="arrText(setStore.allProvinceList, row.regionCode)" /></view>
						</view>
					</template>
					<template v-slot:goods="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">名称：<text class="table-td-text">{{row.productName}}</text></view>
							<view class="table-td-row">类型：<text class="table-td-text">{{row.typeName}}</text></view>
						</view>
					</template>
					<template v-slot:other="{ row }">
						<view class="table-td-cell">
							<view class="table-td-row">单位：<text class="table-td-text">{{row.unit}}</text></view>
							<!-- <view class="table-td-row main-flex">规格：<uni-tag size="small" type="success" :text="row.moreSpec === 0 ? '单规格' : '多规格'" /></view> -->
							<view class="table-td-row main-flex" v-if="checkPermi(['zhouyz:purchaseOrder:price'])">采购价格：<text class="table-td-text">{{row.purchasePrice}}</text></view>
						</view>
					</template>
					<template v-slot:operate="{ row }">
						<view class="table-td-operate table-cell table-td-row main-flex">
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:supplierProduct:edit'])">
								<up-button
									size="mini"
									type="primary"
									@click.stop="editFun(row.id)">
									<uni-icons class="table-operate-btn-icon" type="compose" size="24rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">修改</view>
								</up-button>
							</view>
							<view class="table-operate-btn main-flex" v-if="checkPermi(['zhouyz:supplierProduct:remove'])">
								<up-button
									class="table-operate-btn main-flex"
									size="mini"
									type="error"
									@click.stop="deleteFun(row)">
									<uni-icons class="table-operate-btn-icon" type="trash" size="20rpx" color="#ffffff"></uni-icons>
									<view class="table-operate-btn-text">删除</view>
								</up-button>
							</view>
						</view>
					</template>
					<template v-slot:empty>
						<view class="table-empty">
							<up-empty mode="data"/>
						</view>
					</template>
				</ty-table>
			</view>
			<!--分页-->
			<view class="pagination" v-show="total > 0">
				<ty-pagination
					:total="total"
					:pageSize="pageSize"
					:currentPage="pageNum"
					:pageSizes="[10, 20, 30, 50]"
					mode="simple"
					layout="first,prev,page,next,last"
					@sizeChange="sizeChange"
					@change="pageChange">
				</ty-pagination>
			</view>
		</view>
	</view>
</template>

<script setup>
import { onLoad } from '@dcloudio/uni-app';
import { computed, onMounted, ref, getCurrentInstance } from 'vue'
import { tab, auth, modal } from "@/plugins"
import { getTitle } from "@/utils/getter"
import { checkPermi, checkRole, checkOnlyPermi, checkOnlyRole } from "@/utils/permission"
import { allProvince } from "@/api/erp_api/region"
import { supplierProduct, delSupplierProduct  } from "@/api/erp_api/purchase"
import { productType } from "@/api/erp_api/product"
import { isObjectEmptySimple, copyText, arrText, previewImg } from '@/utils/ty.js'
import useUserStore from '@/store/modules/user'
import useSetStore from '@/store/modules/set'

	const userStore = useUserStore()
	const setStore = useSetStore()
	const { proxy } = getCurrentInstance()

	// 标题设置
	const title = ref('')
	const supplierId = ref('')
	const productId = ref('')
	onLoad((option) => {
		title.value = getTitle(userStore.menu)
		supplierId.value = option.supplierId ? option.supplierId : ''
		productId.value = option.productId ? option.productId : ''
		uni.setNavigationBarTitle({
			title: title.value 
		})
		uni.$on('refreshList', () => {
			getList()
		})
	})
	
	// 获取商品类别
	const productTypeList = ref([])
	const getProductType = () => {
		return productType().then(res => {
			productTypeList.value = res.rows.map(item => {
				return {
					...item,
					value: item.name,
					text: `${item.parentName}->${item.name}`
				}
			}).filter(item => item.parentId) || []
		})
	}
	
	// 搜索相关
	const isSearchNull = ref(true)
	const isOpen =  ref(false)
	const clickSearch = (val) => {
		isOpen.value = val
	}
	const closeSearch = (val) => {
		isOpen.value = false
	}
	// 确认搜索
	const searchRef = ref(null)
	const searchModel = ref({
		typeId: '',
		regionCode: '',
		productName: '',
		supplierName: ''
	})
	const searchSubmit = () => {
		pageNum.value = 1
		tableList.value = []
		isOpen.value = false
		searchFirst()
	}
	// 重置搜索
	const resetSubmit = () => {
		// searchRef.value.resetFields()
		searchModel.value = {
			typeId: '',
			regionCode: '',
			productName: '',
			supplierName: ''
		}
 		if (userStore.regionCode) {
			searchModel.value.regionCode = userStore.regionCode
		}
		isSearchNull.value = true
		isOpen.value = false
		searchFirst()
	}
	
	
	// 数据查询
	const pageNum = ref(1)
	const pageSize = ref(10)
	const total = ref(0)
	const tableList = ref([])
	const isLoading = ref(false)
	
	// 表格头部
	const columns = ref([
		{ title: '供应商信息', key: 'supplier' },
		{ title: '商品信息', key: 'goods' },
		{ title: '其他信息', key: 'other' }
	])
	
	// 从第一页查询
	const searchFirst = () => {
		pageNum.value = 1
		tableList.value = []
		isSearchNull.value = isObjectEmptySimple(searchModel.value)
		getList()
	}
	//页数切换
	const pageChange = (page) => {
		pageNum.value = page
		getList()
	}
	//每页条数切换
	const sizeChange = (size) => {
		pageSize.value = size
		getList()
	}
	
	const getList = () => {
		const params = {
			pageNum: pageNum.value,
			pageSize: pageSize.value
		}
		if (searchModel.value.regionCode) {
			params.regionCode = searchModel.value.regionCode
		}
		if (searchModel.value.productName) {
			params.productName = searchModel.value.productName
		}
		if (searchModel.value.supplierName) {
			params.supplierName = searchModel.value.supplierName
		}
		if (searchModel.value.typeId) {
			params.typeId = searchModel.value.typeId
		}
		if (supplierId.value) {
			params.supplierId = supplierId.value
		}
		if (productId.value) {
			params.productId = productId.value
		}
		isLoading.value = true
		uni.showLoading({
			title: '数据加载中...'
		})
		supplierProduct(params).then(res => {
			tableList.value = res.rows || []
			total.value = res.total
			isLoading.value = false
			uni.hideLoading()
    }).catch(() => {
			isLoading.value = false
			uni.hideLoading()
		})
	}
	
	// 点击行
	const rowClick = (row) => {
		uni.navigateTo({
			url: `/pages/purchase/supplierProductDetail?id=${row.id}`
		})
	}
	// 编辑
	const editFun = (id) => {
		uni.navigateTo({
			url: id?`/pages/purchase/supplierProductEdit?id=${id}`:`/pages/purchase/supplierProductEdit`
		})
	}
	//删除
	const deleteFun = (row) => {
		uni.showModal({
			title: '删除',
			content: `是否确认删除${row.productName}?`,
			success: function (res) {
				if (res.confirm) {
					deleteOne(row.id)
				}
			}
		})
	}
	const deleteOne = (id) => {
		delSupplierProduct({
			id
		}).then(res => {
			modal.showToast('删除成功')
			getList()
		}).catch((err) => {
			modal.showToast(err)
		})
	}
	
	onMounted(async () => {
		if (checkPermi(['zhouyz:productType:list'])) {
			await getProductType()
		}
 		if (userStore.regionCode) {
			searchModel.value.regionCode = userStore.regionCode
		}
		searchFirst()
	})
</script>

<style scoped lang="scss">
.goods {
	align-items: flex-start;
	&_r {
		padding-left: 20rpx;
	}
}
</style>
